home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 January / january_2001.iso / intercd / root / ^4Developers / VB / visbasdb / VBDB / VBDB Code / VB5 Code / Class 6 / Example6-7.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-09-18  |  8.8 KB  |  314 lines

  1. VERSION 5.00
  2. Begin VB.Form frmAuthors 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Authors"
  5.    ClientHeight    =   2625
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   4680
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   2625
  13.    ScaleWidth      =   4680
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin VB.Data datAuthors 
  16.       Caption         =   "Data1"
  17.       Connect         =   "Access"
  18.       DatabaseName    =   "C:\VBDB\Working\Biblio.mdb"
  19.       DefaultCursorType=   0  'DefaultCursor
  20.       DefaultType     =   2  'UseODBC
  21.       Exclusive       =   0   'False
  22.       Height          =   300
  23.       Left            =   2520
  24.       Options         =   0
  25.       ReadOnly        =   0   'False
  26.       RecordsetType   =   1  'Dynaset
  27.       RecordSource    =   "SELECT * FROM Authors ORDER BY Author"
  28.       Top             =   120
  29.       Visible         =   0   'False
  30.       Width           =   1980
  31.    End
  32.    Begin VB.CommandButton cmdDone 
  33.       Caption         =   "Do&ne"
  34.       Height          =   375
  35.       Left            =   3240
  36.       TabIndex        =   13
  37.       TabStop         =   0   'False
  38.       Top             =   2160
  39.       Width           =   1215
  40.    End
  41.    Begin VB.CommandButton cmdDelete 
  42.       Caption         =   "&Delete"
  43.       Height          =   375
  44.       Left            =   1680
  45.       TabIndex        =   12
  46.       TabStop         =   0   'False
  47.       Top             =   2160
  48.       Width           =   1215
  49.    End
  50.    Begin VB.CommandButton cmdEdit 
  51.       Caption         =   "&Edit"
  52.       Height          =   375
  53.       Left            =   120
  54.       TabIndex        =   11
  55.       TabStop         =   0   'False
  56.       Top             =   2160
  57.       Width           =   1215
  58.    End
  59.    Begin VB.CommandButton cmdCancel 
  60.       Caption         =   "&Cancel"
  61.       Height          =   375
  62.       Left            =   3240
  63.       TabIndex        =   10
  64.       TabStop         =   0   'False
  65.       Top             =   1680
  66.       Width           =   1215
  67.    End
  68.    Begin VB.CommandButton cmdSave 
  69.       Caption         =   "&Save"
  70.       Height          =   375
  71.       Left            =   1680
  72.       TabIndex        =   9
  73.       TabStop         =   0   'False
  74.       Top             =   1680
  75.       Width           =   1215
  76.    End
  77.    Begin VB.CommandButton cmdAddNew 
  78.       Caption         =   "&Add New"
  79.       Height          =   375
  80.       Left            =   120
  81.       TabIndex        =   8
  82.       TabStop         =   0   'False
  83.       Top             =   1680
  84.       Width           =   1215
  85.    End
  86.    Begin VB.CommandButton cmdNext 
  87.       Caption         =   "Next =>"
  88.       Height          =   255
  89.       Left            =   2520
  90.       TabIndex        =   7
  91.       TabStop         =   0   'False
  92.       Top             =   1320
  93.       Width           =   1215
  94.    End
  95.    Begin VB.CommandButton cmdPrevious 
  96.       Caption         =   "<= Previous"
  97.       Height          =   255
  98.       Left            =   1080
  99.       TabIndex        =   6
  100.       TabStop         =   0   'False
  101.       Top             =   1320
  102.       Width           =   1215
  103.    End
  104.    Begin VB.TextBox txtYearBorn 
  105.       DataField       =   "Year Born"
  106.       DataSource      =   "datAuthors"
  107.       Height          =   285
  108.       Left            =   1200
  109.       Locked          =   -1  'True
  110.       MaxLength       =   4
  111.       TabIndex        =   2
  112.       Text            =   "Text"
  113.       Top             =   840
  114.       Width           =   1215
  115.    End
  116.    Begin VB.TextBox txtAuthor 
  117.       DataField       =   "Author"
  118.       DataSource      =   "datAuthors"
  119.       Height          =   285
  120.       Left            =   1200
  121.       Locked          =   -1  'True
  122.       TabIndex        =   1
  123.       Text            =   "Text2"
  124.       Top             =   480
  125.       Width           =   3375
  126.    End
  127.    Begin VB.TextBox txtAuthorID 
  128.       DataField       =   "Au_ID"
  129.       DataSource      =   "datAuthors"
  130.       Height          =   285
  131.       Left            =   1200
  132.       Locked          =   -1  'True
  133.       TabIndex        =   3
  134.       TabStop         =   0   'False
  135.       Text            =   "Text1"
  136.       Top             =   120
  137.       Width           =   1215
  138.    End
  139.    Begin VB.Label Label3 
  140.       Caption         =   "Year Born"
  141.       Height          =   255
  142.       Left            =   120
  143.       TabIndex        =   5
  144.       Top             =   840
  145.       Width           =   1215
  146.    End
  147.    Begin VB.Label Label2 
  148.       Caption         =   "Author Name"
  149.       Height          =   255
  150.       Left            =   120
  151.       TabIndex        =   4
  152.       Top             =   480
  153.       Width           =   1215
  154.    End
  155.    Begin VB.Label Label1 
  156.       Caption         =   "Author ID"
  157.       Height          =   255
  158.       Left            =   120
  159.       TabIndex        =   0
  160.       Top             =   120
  161.       Width           =   1215
  162.    End
  163. Attribute VB_Name = "frmAuthors"
  164. Attribute VB_GlobalNameSpace = False
  165. Attribute VB_Creatable = False
  166. Attribute VB_PredeclaredId = True
  167. Attribute VB_Exposed = False
  168. Option Explicit
  169. Private Sub ValidateData(AllOK As Boolean)
  170. Dim Message As String
  171. Dim InputYear As Integer, CurrentYear As Integer
  172. AllOK = True
  173. Message = ""
  174. 'Check for name
  175. If Len(txtAuthor.Text) = 0 Then
  176.   Message = "You must enter an Author Name." + vbCrLf
  177.   txtAuthor.SetFocus
  178.   AllOK = False
  179. End If
  180. 'Check length and range on Year Born
  181. InputYear = Val(txtYearBorn.Text)
  182. CurrentYear = Val(Format(Now, "yyyy"))
  183. If Len(txtYearBorn.Text) <> 0 Then
  184.   If InputYear > CurrentYear Or InputYear < CurrentYear - 150 Then
  185.     Message = Message + "Year Born must be between" & Str(CurrentYear - 150) & " and" & Str(CurrentYear) & "."
  186.     txtYearBorn.SetFocus
  187.     AllOK = False
  188.   End If
  189. End If
  190. If Not (AllOK) Then
  191.   MsgBox Message, vbOKOnly + vbInformation, "Validation Error"
  192. End If
  193. End Sub
  194. Private Sub cmdAddNew_Click()
  195. On Error GoTo HandleErrors
  196. Call SetState("Add")
  197. Exit Sub
  198. HandleErrors:
  199. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  200. Case vbAbort
  201.   Exit Sub
  202. Case vbRetry
  203.   Resume
  204. Case vbIgnore
  205.   Resume Next
  206. End Select
  207. End Sub
  208. Private Sub cmdCancel_Click()
  209. Call SetState("View")
  210. End Sub
  211. Private Sub cmdDelete_Click()
  212. Dim Response As Integer
  213. On Error GoTo HandleErrors
  214. Response = MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete")
  215. If Response = vbNo Then
  216.   Exit Sub
  217. End If
  218. Exit Sub
  219. HandleErrors:
  220. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  221. Case vbAbort
  222.   Exit Sub
  223. Case vbRetry
  224.   Resume
  225. Case vbIgnore
  226.   Resume Next
  227. End Select
  228. End Sub
  229. Private Sub cmdEdit_Click()
  230. Call SetState("Edit")
  231. End Sub
  232. Private Sub cmdNext_Click()
  233. datAuthors.Recordset.MoveNext
  234. If datAuthors.Recordset.EOF Then
  235.   Beep
  236.   datAuthors.Recordset.MoveLast
  237. End If
  238. End Sub
  239. Private Sub cmdPrevious_Click()
  240. datAuthors.Recordset.MovePrevious
  241. If datAuthors.Recordset.BOF Then
  242.   Beep
  243.   datAuthors.Recordset.MoveFirst
  244. End If
  245. End Sub
  246. Private Sub cmdSave_Click()
  247. Dim Valid As Boolean
  248. Call ValidateData(Valid)
  249. If Not (Valid) Then Exit Sub
  250. On Error GoTo HandleErrors
  251. MsgBox "Record saved.", vbOKOnly + vbInformation, "Save"
  252. Call SetState("View")
  253. Exit Sub
  254. HandleErrors:
  255. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  256. Case vbAbort
  257.   Exit Sub
  258. Case vbRetry
  259.   Resume
  260. Case vbIgnore
  261.   Resume Next
  262. End Select
  263. End Sub
  264. Private Sub SetState(AppState As String)
  265. Select Case AppState
  266. Case "View"
  267.   txtAuthorID.BackColor = vbWhite
  268.   txtAuthor.Locked = True
  269.   txtYearBorn.Locked = True
  270.   cmdPrevious.Enabled = True
  271.   cmdNext.Enabled = True
  272.   cmdAddNew.Enabled = True
  273.   cmdSave.Enabled = False
  274.   cmdCancel.Enabled = False
  275.   cmdEdit.Enabled = True
  276.   cmdDelete.Enabled = True
  277.   cmdDone.Enabled = True
  278.   txtAuthor.SetFocus
  279. Case "Add", "Edit"
  280.   txtAuthorID.BackColor = vbRed
  281.   txtAuthor.Locked = False
  282.   txtYearBorn.Locked = False
  283.   cmdPrevious.Enabled = False
  284.   cmdNext.Enabled = False
  285.   cmdAddNew.Enabled = False
  286.   cmdSave.Enabled = True
  287.   cmdCancel.Enabled = True
  288.   cmdEdit.Enabled = False
  289.   cmdDelete.Enabled = False
  290.   cmdDone.Enabled = False
  291.   txtAuthor.SetFocus
  292. End Select
  293. End Sub
  294. Private Sub Form_Activate()
  295. Call SetState("View")
  296. End Sub
  297. Private Sub txtAuthor_KeyPress(KeyAscii As Integer)
  298. If KeyAscii = vbKeyReturn Then
  299.   txtYearBorn.SetFocus
  300.   Exit Sub
  301. End If
  302. End Sub
  303. Private Sub txtYearBorn_KeyPress(KeyAscii As Integer)
  304. If KeyAscii = vbKeyReturn Then
  305.   txtAuthor.SetFocus
  306.   Exit Sub
  307. End If
  308. If (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Or KeyAscii = vbKeyBack Then
  309.   Exit Sub
  310.   Beep
  311.   KeyAscii = 0
  312. End If
  313. End Sub
  314.